﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml.Serialization;
using System.Text;

namespace V5_Book.Model {
    public class BookHelper {
        private static readonly BookHelper m_Instance = new BookHelper();
        /// <summary>
        /// 
        /// </summary>
        public static BookHelper Instance {
            get { return m_Instance; }
        }
        /// <summary>
        /// 加载图书列表
        /// </summary>
        public List<ModelBookInfo> ListBookInfo() {
            List<ModelBookInfo> list = new List<ModelBookInfo>();
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = store.OpenFile("booklist.xml", System.IO.FileMode.OpenOrCreate)) {
                try {
                    XmlSerializer xml = new XmlSerializer(typeof(List<ModelBookInfo>));
                    list = xml.Deserialize(stream) as List<ModelBookInfo>;
                }
                catch (Exception) {

                }
            }
            return list;
        }
        /// <summary>
        /// 保存图书列表
        /// </summary>
        /// <param name="list"></param>
        public void SaveListBookInfo(List<ModelBookInfo> list, ModelBookInfo model) {
            List<ModelBookInfo> listTemp = new List<ModelBookInfo>();
            if (model != null) {
                foreach (var l in list) {
                    ModelBookInfo m = new ModelBookInfo();
                    m = l;
                    if (l.BookName == model.BookName) {
                        m = model;
                    }
                    listTemp.Add(m);
                }
            }
            else {
                listTemp = list;
            }
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = store.OpenFile("booklist.xml", System.IO.FileMode.Create)) {
                XmlSerializer xml = new XmlSerializer(typeof(List<ModelBookInfo>));
                xml.Serialize(stream, listTemp);
            }
        }

        /// <summary>
        /// 添加书籍
        /// </summary>
        public void AddBookInfo(string filePath, string fileContent) {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            using (StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream(filePath, FileMode.Create, FileAccess.Write, myIsolatedStorage))) {
                string someTextData = fileContent;
                writeFile.WriteLine(someTextData);
                writeFile.Close();
            }
        }
        /// <summary>
        /// 读取书籍
        /// </summary>
        public string ReadBookInfo(string filePath) {
            string bookContent = string.Empty;
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile(filePath, FileMode.OpenOrCreate, FileAccess.Read);
            using (StreamReader reader = new StreamReader(fileStream)) {
                bookContent = reader.ReadLine();
            }
            return bookContent;
        }

        /// <summary>
        /// 获取图书设置
        /// </summary>
        public ModelBookInfoSettings GetBookInfoSettings() {
            ModelBookInfoSettings model = new ModelBookInfoSettings();
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            using (IsolatedStorageFileStream stream = store.OpenFile("booksettings.xml", System.IO.FileMode.OpenOrCreate)) {
                try {
                    XmlSerializer xml = new XmlSerializer(typeof(List<ModelBookInfo>));
                    model = xml.Deserialize(stream) as ModelBookInfoSettings;
                }
                catch (Exception) {

                }
            }
            return model;
        }
        /// <summary>
        /// 保存图书设置
        /// </summary>
        /// <param name="model"></param>
        public void SaveBookSettings(ModelBookInfoSettings model) {

        }
    }
}
